#include "Player.h"

#include "TileEngine.h"

#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"


Player::Player(){
	//	Load Textures
	_player = SGD::GraphicsManager::GetInstance()->LoadTexture(L"resource/graphics/char.png");

	//	Setup player data
	m_PlayerPos = SGD::Point(0.0f, 0.0f);
	m_SmellRadius = 256.0f;
	SetHealth(100.0f);
	SetStamina(100.0f);
	m_szSize = SGD::Size(20, 20);
	m_szSmellSize = SGD::Size(50, 50);

	m_recSmellRect = SGD::Rectangle(m_PlayerPos.x - m_CamOffset.x - m_SmellRadius,
		m_PlayerPos.y - m_CamOffset.y - m_SmellRadius,
		(m_PlayerPos.x + 64) - m_CamOffset.x + m_SmellRadius,
		(m_PlayerPos.y + 64) - m_CamOffset.y + m_SmellRadius);
	
	
}

Player::~Player(){
	SGD::GraphicsManager::GetInstance()->UnloadTexture(_player);
}

void Player::Update(float elapsedTime){

	SGD::InputManager* input = SGD::InputManager::GetInstance();

#pragma region //Create Smell Recs
	if (true)
	{
		m_fSmellCreateControl += elapsedTime;
		if (m_fSmellCreateControl > SMELL_CREATE_TIMER)
		{
			m_fSmellCreateControl = 0;
			StankNode* NewNode = CreateSmellNode(this);

			m_ListSmellTrail.push_back(NewNode);
		}
	}
#pragma endregion

#pragma region //Smell Rec Clean up

	if (m_ListSmellTrail.size() > 0)
	{
		auto Stank_iter = m_ListSmellTrail.begin();

		for (; Stank_iter != m_ListSmellTrail.end(); Stank_iter++)
		{
			auto TempIter = Stank_iter;

			if ((*Stank_iter)->Update(elapsedTime) == false)
			{
				m_ListSmellTrail.remove(*TempIter);
				Stank_iter = m_ListSmellTrail.begin();

				if (Stank_iter == m_ListSmellTrail.end())
					break;
			}
		}

	}
#pragma endregion

#pragma region //player Input

	SGD::Size wsize = SGD::Size(320.0f, 320.0f);

	if (input->IsKeyDown(SGD::Key::LeftShift)){
		if (input->IsKeyDown(SGD::Key::W) && m_PlayerPos.y > 0.0f)
			m_PlayerPos.y -= 1.75f;

		if (input->IsKeyDown(SGD::Key::A) && m_PlayerPos.x > 0.0f)
			m_PlayerPos.x -= 1.75f;

		if (input->IsKeyDown(SGD::Key::S) && m_PlayerPos.y + 32.0f < wsize.height)
			m_PlayerPos.y += 1.75f;

		if (input->IsKeyDown(SGD::Key::D) && m_PlayerPos.x + 32.0f < wsize.width)
			m_PlayerPos.x += 1.75f;
	}
	else{
		if (input->IsKeyDown(SGD::Key::W) && m_PlayerPos.y > 0.0f)
			m_PlayerPos.y -= 1.0f;

		if (input->IsKeyDown(SGD::Key::A) && m_PlayerPos.x > 0.0f)
			m_PlayerPos.x -= 1.0f;

		if (input->IsKeyDown(SGD::Key::S) && m_PlayerPos.y + 32.0f < wsize.height)
			m_PlayerPos.y += 1.0f;

		if (input->IsKeyDown(SGD::Key::D) && m_PlayerPos.x + 32.0f < wsize.width)
			m_PlayerPos.x += 1.0f;


		m_ptPosition = m_PlayerPos;
	}
#pragma endregion 

	int ba;

	m_recSmellRect = SGD::Rectangle(m_PlayerPos.x - m_CamOffset.x - m_SmellRadius,
		m_PlayerPos.y - m_CamOffset.y - m_SmellRadius,
		(m_PlayerPos.x + 64) - m_CamOffset.x + m_SmellRadius,
		(m_PlayerPos.y + 64) - m_CamOffset.y + m_SmellRadius);



	
	Entity::Update(elapsedTime);

}

void Player::Render(){


	//why is this hiting before the Main update for the player?**
	SGD::GraphicsManager* graphics = SGD::GraphicsManager::GetInstance();


	m_recCollide = SGD::Rectangle(m_PlayerPos, m_szSize);


	graphics->DrawRectangle(m_recCollide, {}, SGD::Color(0, 255, 0));
	graphics->DrawTexture(_player,				//	Offset based on camera position
		SGD::Point(m_PlayerPos.x - m_CamOffset.x, m_PlayerPos.y - m_CamOffset.y),
		0.0f,
		{},
		SGD::Color(255, 255, 255, 255),
		SGD::Size(1.0f, 1.0f));


	//Render Smell Rects Used for Testing	
#if 0 
	if (m_ListSmellTrail.size() > 0)
	{
		auto Smell_iter = m_ListSmellTrail.begin();
		int count = 0;

		for (; Smell_iter != m_ListSmellTrail.end(); Smell_iter++)
		{
			
			graphics->DrawRectangle((*Smell_iter)->m_recStankVolume, {}, SGD::Color(155, 255, 0));
			count++;
		}
	}
#endif

	
}

void Player::HandleCollision(const IEntity* pOther){

	int size;

	size = 1;

}


SGD::Rectangle Player::GetRect() const {
	return  m_recCollide;
}